package com.wuyan.locationdog.ui.my

import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingFragment
import com.arcsoft.commonbase.ext.createEmptyView
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.OrderListAdapter
import com.wuyan.locationdog.databinding.MyOrderFragmentBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.help.finishRefreshAndLoadMoreNoMoreData
import com.wuyan.locationdog.viewmodel.MyViewModel


class MyOrderFragment(var index: Int) : BaseBindingFragment<MyOrderFragmentBinding>() {

    private val mViewModel: MyViewModel by viewModels()

    private val mAdapter by lazy { OrderListAdapter(R.layout.item_order_list, null) }

    override fun lazyLoadData() {


        binding.apply {
            setOnRefreshLoadMore(smartRefreshLayout)


            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无订单", com.arcsoft.commonbase.R.mipmap.empty_order))


                    addChildClickViewIds(R.id.itemLayout,R.id.btnPay)
                    setOnItemChildClickListener { adapter, view, position ->
                        DeviceHelp.instantOrderListBeanContent = data[position]
                        ActivityUtils.startActivity(MyOrderDetailActivity::class.java)
                    }
                }
            }


        }
    }

    override fun loadNetData() {
        super.loadNetData()

        val map = HashMap<String, Any>()
        map["pageSize"] = 10
        map["pageNum"] = currentPage
        //订单状态 1待付款 5已完成 6已关闭
        if (index == 1) {
            map["orderStatus"] = 1
        } else if (index == 2) {
            map["orderStatus"] = 6
        } else if (index == 3) {
            map["orderStatus"] = 5
        }


        mViewModel.myOrderList(map)

    }

    override fun observe() {
        super.observe()
        mViewModel.orderListBean.observe(this) {
            finishRefreshAndLoadMoreNoMoreData(binding.smartRefreshLayout, currentPage, it.total)
            if (currentPage == 1) {
                mAdapter.setNewInstance(it.content.toMutableList())
            } else {
                mAdapter.addData(it.content.toMutableList())
            }
        }

    }

}